﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    public partial class OAuth2AccountService
    {
        /// 增加一条数据
        /// </summary>
        public bool Add(OAuth2Account model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into OAuth2Account(");
            strSql.Append("OAuthServer,Token,OpenID,ExpireTime,NickName,HeadUrl,BindAccount)");
            strSql.Append(" values (");
            strSql.Append("@OAuthServer,@Token,@OpenID,@ExpireTime,@NickName,@HeadUrl,@BindAccount)");
            SqlParameter[] parameters = {
					new SqlParameter("@OAuthServer", SqlDbType.NVarChar,200),
					new SqlParameter("@Token", SqlDbType.NVarChar,200),
					new SqlParameter("@OpenID", SqlDbType.NVarChar,200),
					new SqlParameter("@ExpireTime", SqlDbType.DateTime),
					new SqlParameter("@NickName", SqlDbType.NVarChar,200),
					new SqlParameter("@HeadUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@BindAccount", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.OAuthServer;
            parameters[1].Value = model.Token;
            parameters[2].Value = model.OpenID;
            parameters[3].Value = model.ExpireTime;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.HeadUrl;
            parameters[6].Value = model.BindAccount;
            int result = 0;
            result= DBHelper.ExecuteNonQuery(strSql.ToString(),parameters);
            if (result > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(OAuth2Account model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update OAuth2Account set ");
            strSql.Append("OAuthServer=@OAuthServer,");
            strSql.Append("Token=@Token,");
            strSql.Append("OpenID=@OpenID,");
            strSql.Append("ExpireTime=@ExpireTime,");
            strSql.Append("NickName=@NickName,");
            strSql.Append("HeadUrl=@HeadUrl,");
            strSql.Append("BindAccount=@BindAccount");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@OAuthServer", SqlDbType.NVarChar,200),
					new SqlParameter("@Token", SqlDbType.NVarChar,200),
					new SqlParameter("@OpenID", SqlDbType.NVarChar,200),
					new SqlParameter("@ExpireTime", SqlDbType.DateTime),
					new SqlParameter("@NickName", SqlDbType.NVarChar,200),
					new SqlParameter("@HeadUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@BindAccount", SqlDbType.NVarChar,200),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.OAuthServer;
            parameters[1].Value = model.Token;
            parameters[2].Value = model.OpenID;
            parameters[3].Value = model.ExpireTime;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.HeadUrl;
            parameters[6].Value = model.BindAccount;
            parameters[7].Value = model.ID;

            return DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(OAuthServer OAuthServer, string OpenID)
        {
            bool result =false;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM OAuth2Account");
            strSql.Append(" WHERE OAuthServer = @OAuthServer AND OpenID = @OpenID");
            SqlParameter[] parameters = {
					new SqlParameter("@OAuthServer",OAuthServer),
                    new SqlParameter("@OpenID",OpenID)
			};

            object obj = DBHelper.ExecuteScalar(strSql.ToString(),parameters);
            if(obj!=null&& obj!=DBNull.Value)
            {
                result =true;
            }
            else
            {
                result =false;
            }
            return result;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OAuth2Account GetModel(OAuthServer OAuthServer, string OpenID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,OAuthServer,Token,OpenID,ExpireTime,NickName,HeadUrl,BindAccount from OAuth2Account ");
            strSql.Append(" WHERE OAuthServer = @OAuthServer AND OpenID = @OpenID");
            SqlParameter[] parameters = {
					new SqlParameter("@OAuthServer",OAuthServer),
                    new SqlParameter("@OpenID",OpenID)
			};

            OAuth2Account model = new OAuth2Account();
            SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters);
            if (dr.Read())
            {
                DataRowToModel(dr);
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OAuth2Account DataRowToModel(SqlDataReader row)
        {
            OAuth2Account model = new OAuth2Account();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["OAuthServer"] != null)
                {
                    model.OAuthServer = row["OAuthServer"].ToString();
                }
                if (row["Token"] != null)
                {
                    model.Token = row["Token"].ToString();
                }
                if (row["OpenID"] != null)
                {
                    model.OpenID = row["OpenID"].ToString();
                }
                if (row["ExpireTime"] != null && row["ExpireTime"].ToString() != "")
                {
                    model.ExpireTime = DateTime.Parse(row["ExpireTime"].ToString());
                }
                if (row["NickName"] != null)
                {
                    model.NickName = row["NickName"].ToString();
                }
                if (row["HeadUrl"] != null)
                {
                    model.HeadUrl = row["HeadUrl"].ToString();
                }
                if (row["BindAccount"] != null)
                {
                    model.BindAccount = row["BindAccount"].ToString();
                }
            }
            return model;
        }


    }
}
